package com.jluzh.server;

import com.jluzh.bean.Employee;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.MultivaluedMap;
import java.util.Set;

/**
 * @author EvanHuang
 * @date 11/5/2018 9:30 AM
 * @since
 */
@Path("params")
public class ParamsTest {
    @GET
    @Path("{id}/summary/{month}")
    public String pathParam2(@PathParam("id") Long id, @PathParam("month")int month) {
        System.out.println(id);
        System.out.println(month);
        return "success";
    }

    @GET
    @Path("/query")
    public String queryParam(@QueryParam("name") String name) {
        System.out.println(name);
        return "success";
    }

    /**
     * POST param/hobby?hobby=java&hobby=haha&hobby=hehe
     * @FormParam注解只能接受x-www-form-urlencoded编码格式的form表单提交，这个是必要条件；
     * @param hobbies
     * @return
     */
    @POST
    @Path("/hobby")
    @Produces(MediaType.APPLICATION_JSON)
    public Employee formParam(@FormParam("hobby") Set<String> hobbies) {
        System.out.println(hobbies);
        return new Employee();
    }

    @Path("/form")
    @Produces(MediaType.APPLICATION_JSON)
    public Employee formParam(@FormParam("name") String name,
                              @DefaultValue("18") @FormParam("age") int age) {
        return new Employee(1L, name, age);
    }

    @POST
    @Path("/formpojo")
    @Produces(MediaType.APPLICATION_JSON)
    public Employee formPojoParam(@BeanParam Employee e) {
        e.setId(2L);
        return e;
    }

    // 直接获取表单内容
    @POST
    @Path("/formui2")
    public String formPojoParam(MultivaluedMap<String, String> fs) {
        System.out.println(fs);
        return "success";
    }

    /**
     * 通过@HeaderParam获取请求头内容
     */
    @GET
    @Path("/head")
    public String headParam(@HeaderParam("token") String token) {
        System.out.println(token);
        return "success";
    }

    // 分页
    // url:  http://localhost:8081/resources/params/matrix;currentPage=2;pageSize=10;keyword=k
    @GET
    @Path("/matrix")
    public String matrix(@MatrixParam("currentPage") int currentPage, @MatrixParam("pageSize") int pageSize,
                         @MatrixParam("keyword") String keyword) {
        System.out.println("currentPage:" + currentPage);
        System.out.println("pageSize:" + pageSize);
        System.out.println("keyword:" + keyword);
        return "success";
    }


}
